const cloud = require('wx-server-sdk')

cloud.init()

const SUCCESS = 0;
const BAD_PARAM = -1;
const NOT_FOUND = -2;

/// (id: DeliveryId) -> (StatusCode)
exports.main = async (event) => {
  const id = event.id;

  if (id == null) {
    return {
      code: BAD_PARAM
    };
  }

  const db = cloud.database();
  try {
    await db.collection('deliveries').doc(id).update({
      data: {
        state: 'processing'
      }
    });
    return {
      code: SUCCESS
    };
  } catch (e) {
    return {
      code: NOT_FOUND
    };
  }
}
